package com.xiaoxin.service;

import cn.hutool.core.collection.CollUtil;
import com.xiaoxin.autoconfig.template.HuanXinTemplate;
import com.xiaoxin.commons.utils.Constants;
import com.xiaoxin.dubbo.api.CommentApi;
import com.xiaoxin.dubbo.api.FriendApi;
import com.xiaoxin.dubbo.api.UserApi;
import com.xiaoxin.dubbo.api.UserInfoApi;
import com.xiaoxin.exception.BusinessException;
import com.xiaoxin.interceptor.UserHolder;
import com.xiaoxin.model.domain.User;
import com.xiaoxin.model.domain.UserInfo;
import com.xiaoxin.model.enums.CommentType;
import com.xiaoxin.model.mongo.Comment;
import com.xiaoxin.model.mongo.Friend;
import com.xiaoxin.model.vo.*;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author xiaoxiaode
 * @date 2021-09-07-20:47
 **/
@SuppressWarnings("all")
@Service
public class MessageService {

    @DubboReference
    private UserApi userApi;

    @DubboReference
    private UserInfoApi userInfoApi;

    @DubboReference
    private FriendApi friendApi;

    @Autowired
    private HuanXinTemplate huanXinTemplate;

    @DubboReference
    private CommentApi commentApi;

    /*
     * 根据环信查询用户信息
     * */
    public UserInfoVo findByHuanXin(String huanxinId) {
        User user = userApi.findByhuanxinId(huanxinId);
        UserInfo userInfo = userInfoApi.findById(user.getId());
        UserInfoVo vo = new UserInfoVo();
        BeanUtils.copyProperties(userInfo, vo);//copy同名同类型的属性
        if (userInfo.getAge() != null) {
            vo.setAge(userInfo.getAge().toString());
        }
        return vo;
    }

    /*
     * 添加联系人
     * */
    public void contacts(Long friendId) {
        // 将好友关系注册到环信
        String myId = Constants.HX_USER_PREFIX + UserHolder.getId();
        String friId = Constants.HX_USER_PREFIX + friendId;
        Boolean contact = huanXinTemplate.addContact(myId, friId);
        if (!contact) {
            throw new BusinessException(ErrorResult.error());
        }
        // 注册成功记录好友关系到mongodb
        friendApi.save(UserHolder.getId(), friendId);
    }

    /*
     * 联系人列表
     * */
    public PageResult contactFriends(Integer page, Integer pagesize, String keyword) {
        // 查询所有好友数据
        Long userId = UserHolder.getId();
        List<Friend> friends = friendApi.findFriends(userId, page, pagesize);
        if (CollUtil.isEmpty(friends)) {
            return new PageResult();
        }
        // 提取好友id
        List<Long> friendIds = CollUtil.getFieldValues(friends, "friendId", Long.class);
        // 查询好友详情
        UserInfo info = new UserInfo();
        info.setNickname(keyword);
        Map<Long, UserInfo> infoMap = userInfoApi.findByIds(friendIds, info);
        // 构造vo
        List<ContactVo> list = new ArrayList<>();
        for (Friend friend : friends) {
            UserInfo userInfo = infoMap.get(friend.getFriendId());
            if (userInfo != null) {
                ContactVo vo = ContactVo.init(userInfo);
                list.add(vo);
            }
        }
        return new PageResult(page, pagesize, 0L, list);
    }

    /*
     * 点赞人列表
     * */
    public PageResult likes(Integer page, Integer pagesize) {
        List<Comment> comments = commentApi.findById(UserHolder.getId(), CommentType.LIKE);
        return getPageResult(page, pagesize, comments);
    }

    /*
     * 抽取公共获取分页结果集
     * */
    private PageResult getPageResult(Integer page, Integer pagesize, List<Comment> comments) {
        if (CollUtil.isEmpty(comments)) {
            return new PageResult();
        }
        List<Long> ids = CollUtil.getFieldValues(comments, "userId", Long.class);
        Map<Long, UserInfo> infoMap = userInfoApi.findByIds(ids, null);
        List<CommentVo> vos = new ArrayList<>();
        for (Comment comment : comments) {
            UserInfo userInfo = infoMap.get(comment.getUserId());
            if (userInfo != null) {
                CommentVo vo = CommentVo.init(userInfo, comment);
                vos.add(vo);
            }
        }
        return new PageResult(page, pagesize, 0L, vos);
    }

    /*
     * 喜欢我的人列表
     * */
    public PageResult loves(Integer page, Integer pagesize) {
        List<Comment> comments = commentApi.findById(UserHolder.getId(), CommentType.LOVE);
        return getPageResult(page, pagesize, comments);
    }

    /*
     * 评论我的人列表
     * */
    public PageResult comments(Integer page, Integer pagesize) {
        List<Comment> comments = commentApi.findById(UserHolder.getId(), CommentType.COMMENT);
        return getPageResult(page, pagesize, comments);
    }

    /*
     * card不喜欢-删除好友
     * */
    public void remove(Long unlikeUserId) {
        String myId = Constants.HX_USER_PREFIX + UserHolder.getId();
        String friId = Constants.HX_USER_PREFIX + unlikeUserId;
        Boolean contact = huanXinTemplate.deleteContact(myId, friId);
        if (!contact) {
            throw new BusinessException(ErrorResult.error());
        }
        // 注册成功记录好友关系到mongodb
        friendApi.remove(UserHolder.getId(), unlikeUserId);
    }

}
